user.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. const app = getApp();
  2. const api = require("../../../request/api");
  3. const http = require("../../../request/http");
  4. const utils = require("../../../utils/util.js");
  5. const storeKeys = require("../../../utils/storageKeys.js");
  6. Page({
  7. data: {
  8. showModal: false, //是否显示奖励金说明弹窗
  9. showWithdrawalModal: false, //是否显示提现提示
  10. showNicknameModal: false,
  11. isLoading: true, //是否处于加载
  12. sHome: false,
  13. isLogOn: false, //是否已登录
  14. info: {},
  15. todoCounts: {}, //待处理订单数
  16. updatingNickname: "",
  17. },
  18. onLoad: function (options) {},
  19. onShow() {
  20. if (!utils.getStorageSync(storeKeys.TOKEN)) {
  21. this.setData({
  22. isLogOn: false,
  23. isLoading: false,
  24. });
  25. } else {
  26. this.setData({
  27. isLogOn: true,
  28. });
  29. this.getUserInfo();
  30. this.getTodoCounts();
  31. }
  32. this.changePageHaed();
  33. app.changeTabBarBadge(utils.getStorageSync(storeKeys.CARETOTAl));
  34. },
  35. changePageHaed() {
  36. let token = utils.getStorageSync(storeKeys.TOKEN);
  37. if (token) {
  38. if (this.data.info.chara) {
  39. wx.setNavigationBarColor({
  40. frontColor: "#ffffff",
  41. backgroundColor: "#DF3120",
  42. });
  43. } else {
  44. wx.setNavigationBarColor({
  45. frontColor: "#000000",
  46. backgroundColor: "#F7F7F7",
  47. });
  48. }
  49. } else {
  50. wx.setNavigationBarColor({
  51. frontColor: "#000000",
  52. backgroundColor: "#F7F7F7",
  53. });
  54. }
  55. },
  56. //获取用户信息
  57. getUserInfo() {
  58. http.request({
  59. url: api.URL + "/api/user/info",
  60. method: "GET",
  61. token: utils.getStorageSync(storeKeys.TOKEN),
  62. success: res => {
  63. let info = res.data.data.userInfo;
  64. // 开发专用
  65. if (info.mobile == "17573207179") {
  66. // info.show_cash_box = true;
  67. // info.role = 1;
  68. // info.seller_grade = 3;
  69. info.grade_id = 10001;
  70. info.is_pickup = 1;
  71. }
  72. //是否为特殊身份
  73. let chara = false;
  74. if ((info.role && info.role != 1) || info.show_cash_box) {
  75. chara = true;
  76. }
  77. info.chara = chara;
  78. utils.setStorageSync(storeKeys.USERINFO, info);
  79. this.setData({
  80. info,
  81. });
  82. this.changePageHaed();
  83. setTimeout(() => {
  84. this.setData({
  85. isLoading: false,
  86. });
  87. }, 300);
  88. },
  89. error: res => {
  90. utils.toast(res.data.message);
  91. },
  92. });
  93. },
  94. //获取用户待处理订单数
  95. getTodoCounts() {
  96. http.request({
  97. url: api.URL + "/api/order/todoCounts",
  98. method: "GET",
  99. token: utils.getStorageSync(storeKeys.TOKEN),
  100. success: res => {
  101. this.setData({
  102. todoCounts: res.data.data.counts || {},
  103. });
  104. },
  105. error: res => {
  106. utils.toast(res.data.message);
  107. },
  108. });
  109. },
  110. navigateTo(e) {
  111. app.navigateTo(e);
  112. },
  113. //需要登陆进入的页面
  114. navigateToToken: utils.throttle(function (e) {
  115. if (utils.getStorageSync(storeKeys.TOKEN)) {
  116. app.navigateTo(e);
  117. } else {
  118. wx.navigateTo({
  119. url: "/pages/tabBar/login/login",
  120. });
  121. }
  122. }),
  123. //提现
  124. withdrawal: utils.throttle(function (e) {
  125. if (utils.getStorageSync(storeKeys.TOKEN)) {
  126. let ktxyj_amount = parseFloat(this.data.info.ktxyj_amount);
  127. if (!ktxyj_amount || ktxyj_amount <= 0) {
  128. utils.toast("暂无可提现金额");
  129. return;
  130. }
  131. wx.navigateTo({
  132. url: "/pages/user/pages/withdrawal/withdrawal",
  133. });
  134. } else {
  135. wx.navigateTo({
  136. url: "/pages/tabBar/login/login",
  137. });
  138. }
  139. }),
  140. //前往分销中心
  141. goTopPomotionCenter: utils.throttle(function (e) {
  142. let role = this.data.info.role;
  143. //1 :普通用户
  144. let url =
  145. "/pages/user/pages/store/promotionCenter/promotionCenter?role=" + role;
  146. if (role == 1) {
  147. url = "/pages/user/pages/store/applyRetail/applyRetail";
  148. }
  149. wx.navigateTo({
  150. url: url,
  151. });
  152. }),
  153. //显示奖励金说明弹窗
  154. showModal() {
  155. this.setData({
  156. showModal: true,
  157. });
  158. },
  159. //隐藏奖励金说明弹窗
  160. hideModal() {
  161. this.setData({
  162. showModal: false,
  163. });
  164. },
  165. //显示提现提示
  166. showWithdrawalModal() {
  167. this.setData({
  168. showWithdrawalModal: true,
  169. });
  170. },
  171. //隐藏提现提示
  172. hideWithdrawalModal() {
  173. this.setData({
  174. showWithdrawalModal: false,
  175. });
  176. },
  177. //前往我的分享码
  178. goToPoster: utils.throttle(function (e) {
  179. let url = "/pages/user/pages/appletIncomePoster/appletIncomePoster";
  180. wx.navigateTo({
  181. url: url,
  182. });
  183. }),
  184. //授权获取微信信息
  185. getUserProfile: utils.throttle(function (e) {
  186. wx.getUserProfile({
  187. desc: "用于头像展示",
  188. success: res => {
  189. this.updateUserInfo(res.userInfo.avatarUrl, res.userInfo.nickName);
  190. },
  191. });
  192. }),
  193. updateUserInfo(avatarUrl, nickName, updateType = "") {
  194. let data = {
  195. avatarUrl,
  196. nickName,
  197. ...(updateType ? { type: updateType } : {}),
  198. };
  199. wx.showLoading({
  200. title: "加载中",
  201. mask: true,
  202. });
  203. http.request({
  204. url: api.URL + "/api/user/upd",
  205. method: "POST",
  206. token: utils.getStorageSync(storeKeys.TOKEN),
  207. data,
  208. success: res => {
  209. wx.hideLoading();
  210. this.getUserInfo();
  211. },
  212. error: res => {
  213. wx.hideLoading();
  214. utils.toast(res.data.message);
  215. },
  216. });
  217. },
  218. //开通会员、前往会员中心
  219. memberClick: utils.throttle(function (e) {
  220. //is_member 是否为会员 1是 0否
  221. if (this.data.info.is_member == 1) {
  222. wx.navigateTo({
  223. url: "/pages/member/pages/memberCenter/memberCenter",
  224. });
  225. } else {
  226. wx.navigateTo({
  227. url: "/pages/member/pages/buyMember/buyMember",
  228. });
  229. }
  230. }),
  231. // 福利核销
  232. clickScanCode: utils.throttle(function (e) {
  233. console.log("福利核销");
  234. let _this = this;
  235. wx.scanCode({
  236. success(res) {
  237. console.log(res);
  238. let result = res.result;
  239. try {
  240. wx.navigateTo({
  241. url: "/pages/member/pages/writeOff/writeOff?hxCode=" + result,
  242. });
  243. } catch (e) {
  244. utils.toast("二维码错误");
  245. }
  246. },
  247. fail() {},
  248. });
  249. }),
  250. showNicknameInputModal() {
  251. this.setData({
  252. updatingNickname: this.data.info.nickName || "",
  253. showNicknameModal: true,
  254. });
  255. },
  256. hideNicknameModal() {
  257. this.setData({
  258. showNicknameModal: false,
  259. });
  260. },
  261. updateNickName() {
  262. this.updateUserInfo("", this.data.updatingNickname, "1");
  263. this.hideNicknameModal();
  264. },
  265. onChooseAvatar(e) {
  266. const { avatarUrl } = e.detail;
  267. const _this = this;
  268. wx.showLoading({
  269. title: "上传中",
  270. mask: true,
  271. });
  272. wx.uploadFile({
  273. url: api.URL + "/api/upload/image",
  274. filePath: avatarUrl,
  275. name: "file",
  276. header: {
  277. "Access-Token": utils.getStorageSync(storeKeys.TOKEN),
  278. },
  279. success: function (res) {
  280. let data = JSON.parse(res.data);
  281. //重新上传
  282. wx.showToast({
  283. title: "上传成功",
  284. icon: "success",
  285. duration: 1500,
  286. });
  287. _this.updateUserInfo(data.data.fileInfo.preview_url, "", "2");
  288. wx.hideLoading();
  289. },
  290. error: function (error) {
  291. wx.showToast({
  292. title: "上传失败",
  293. icon: "error",
  294. duration: 1500,
  295. });
  296. wx.hideLoading();
  297. },
  298. });
  299. },
  300. });