index.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. var util = require('../../../utils/util.js');
  2. var api = require('../../../config/api.js');
  3. var user = require('../../../services/user.js');
  4. var app = getApp();
  5. // 定义插件变量 引入方法
  6. const tempUserInfo = {
  7. nickname: 'Hi,游客',
  8. userName: '点击去登录',
  9. avatarUrl: 'https://platform-wxmall.oss-cn-beijing.aliyuncs.com/upload/20180727/150547696d798c.png'
  10. }
  11. Page({
  12. data: {
  13. userInfo: {},
  14. hasMobile: '',
  15. imgServer: util.imgServer,
  16. all: {
  17. icon: 'ct1',
  18. name: '全部订单',
  19. id: -1
  20. },
  21. orderList: [{
  22. icon: 'ct2',
  23. name: '待付款',
  24. id: 0
  25. },
  26. {
  27. icon: 'ct3',
  28. name: '待发货',
  29. id: 201
  30. },
  31. {
  32. icon: 'ct4',
  33. name: '待收货',
  34. id: 300
  35. },
  36. {
  37. icon: 'ct5',
  38. name: '已完成',
  39. id: 301
  40. }
  41. ],
  42. barList: [{
  43. icon: 'cb1',
  44. url: '/pages/ucenter/collect/collect',
  45. name: '我的收藏',
  46. id: 0
  47. },
  48. {
  49. icon: 'cb5',
  50. url: '/pages/coupon/coupon',
  51. name: '优惠券',
  52. id: 5
  53. },
  54. // {
  55. // icon: 'cb6',
  56. // url: '/pages/ucenter/footprint/footprint',
  57. // name: '我的积分',
  58. // id: 1
  59. // },
  60. {
  61. icon: 'cb2',
  62. url: '/pages/ucenter/address/address',
  63. name: '地址管理',
  64. id: 1
  65. }
  66. ],
  67. senceList: [{
  68. icon: 'vr@2x',
  69. url: '/pages/sence/sence',
  70. name: '场景列表',
  71. type: 31
  72. },
  73. {
  74. icon: 'vr_live@2x',
  75. url: '/pages/sence/sence',
  76. name: '带看',
  77. type: 32
  78. },
  79. {
  80. icon: 'vr_live@2x',
  81. url: '/pages/roomManger/roomManger',
  82. name: '我的房间',
  83. type: 33
  84. }
  85. ],
  86. relatedGoods: [],
  87. isLogin: app.globalData.isLogin
  88. },
  89. getGoodsRelated: function () {
  90. let that = this;
  91. util.request(api.GoodsRelated, {}).then(function (res) {
  92. if (res.errno === 0) {
  93. that.setData({
  94. relatedGoods: res.data.goodsList,
  95. });
  96. }
  97. });
  98. },
  99. tapByIdx: function (e) {
  100. let {
  101. type
  102. } = e.currentTarget.dataset;
  103. let {
  104. url
  105. } = e.currentTarget.dataset;
  106. console.log(url + '?type=' + type)
  107. // if (this.data.isLogin) {
  108. wx.navigateTo({
  109. url: url + '?type=' + type,
  110. success: function (res) {},
  111. fail: function (res) {},
  112. complete: function (res) {},
  113. })
  114. // } else {
  115. // wx.navigateTo({
  116. // url: '/pages/auth/btnAuth/btnAuth',
  117. // })
  118. // // wx.showModal({
  119. // // title: '提示',
  120. // // content: '请登录后查看',
  121. // // showCancel: false,
  122. // // confirmColor: '#0075DC',
  123. // // success: res=>{
  124. // // }
  125. // // });
  126. // }
  127. },
  128. onLoad: function (options) {
  129. getApp().checkNetStatu();
  130. // 页面初始化 options为页面跳转所带来的参数
  131. // this.getGoodsRelated()
  132. // wx.setNavigationBarColor({
  133. // frontColor:'#ffffff',
  134. // backgroundColor:'#fb6400',
  135. // "navigationStyle":"custom"
  136. // animation: {
  137. // duration: 400,
  138. // timingFunc:'easeIn'
  139. // }
  140. // })
  141. },
  142. onReady: function () {
  143. },
  144. async getCounts() {
  145. let {
  146. data
  147. } = await util.request(api.OrderCount)
  148. let orderList = this.data.orderList.map(item => {
  149. let ci = data.find(({
  150. code
  151. }) => code === item.id)
  152. if (ci) {
  153. item.count = ci.num > 99 ? '..' : ci.num
  154. }
  155. return {
  156. ...item
  157. }
  158. })
  159. this.setData({
  160. orderList
  161. })
  162. },
  163. async getUserInfo() {
  164. const {
  165. data
  166. } = await util.request(api.UserInfo)
  167. console.log('UserInfo', data)
  168. wx.setStorageSync('userinfoDetail', data)
  169. this.setData({
  170. userInfo: {
  171. // nickName: data.nickname || tempUserInfo.nickName,
  172. nickname: data.nickname ? data.nickname : (data.username || tempUserInfo.nickname),
  173. avatarUrl: data.avatar || tempUserInfo.avatarUrl,
  174. }
  175. })
  176. },
  177. async getBrandList() {
  178. let res = await util.request(api.GetAdminBrands)
  179. let list = res.data
  180. if (list && list.length > 0) {
  181. this.setData({
  182. hasBrand: true
  183. })
  184. } else {
  185. this.setData({
  186. hasBrand: false
  187. })
  188. }
  189. },
  190. onShow: function () {
  191. getApp().updateCardCount()
  192. let userInfo = wx.getStorageSync('userInfo');
  193. let token = wx.getStorageSync('token');
  194. // this.getBrandList()
  195. this.getUserInfo()
  196. this.getCounts()
  197. // 页面显示
  198. if (userInfo && token) {
  199. app.globalData.userInfo = userInfo;
  200. app.globalData.token = token;
  201. }
  202. this.setData({
  203. // userInfo: app.globalData.userInfo,
  204. token: app.globalData.token
  205. });
  206. util.request(api.CartList).then((res) => {
  207. let isLogin = false
  208. if (res.errno === 401) {
  209. isLogin = false
  210. app.globalData.userInfo = tempUserInfo;
  211. app.globalData.token = '';
  212. } else {
  213. isLogin = true
  214. app.globalData.userInfo = userInfo;
  215. app.globalData.token = token;
  216. }
  217. if (!isLogin) {
  218. this.setData({
  219. token: '',
  220. userInfo: tempUserInfo
  221. });
  222. }
  223. this.setData({
  224. isLogin
  225. });
  226. });
  227. if (typeof this.getTabBar === 'function' && this.getTabBar()) {
  228. this.getTabBar().setData({
  229. selected: 3
  230. })
  231. }
  232. },
  233. onPullDownRefresh() {
  234. getApp().onPullDownRefresh()
  235. },
  236. onHide: function () {
  237. // 页面隐藏
  238. },
  239. onUnload: function () {
  240. // 页面关闭
  241. },
  242. bindGetUserInfo(e) {
  243. let userInfo = wx.getStorageSync('userInfo');
  244. let token = wx.getStorageSync('token');
  245. if (userInfo && token) {
  246. return;
  247. }
  248. if (e.detail.userInfo) {
  249. //用户按了允许授权按钮
  250. user.loginByWeixin(e.detail).then(res => {
  251. this.setData({
  252. userInfo: res.data.userInfo,
  253. isLogin: true
  254. });
  255. res.data.userInfo.userId = res.data.userId
  256. res.data.userInfo.isAdmin = res.data.isAdmin
  257. wx.setStorageSync('userInfo', res.data.userInfo);
  258. wx.setStorageSync('token', res.data.token);
  259. wx.setStorageSync('userId', res.data.userId);
  260. app.globalData.userInfo = res.data.userInfo;
  261. app.globalData.token = res.data.token;
  262. }).catch((err) => {
  263. this.setData({
  264. token: '',
  265. userInfo: tempUserInfo
  266. });
  267. app.globalData.userInfo = tempUserInfo;
  268. app.globalData.token = '';
  269. console.log(err)
  270. });
  271. } else {
  272. //用户按了拒绝按钮
  273. // wx.showModal({
  274. // title: '温馨提示',
  275. // content: '您点击了"取消"授权,将无法获取杭州天门科技有限公司完整服务。请“授权”以保证各项功能正常使用。',
  276. // confirmText:'授权',
  277. // success: function (res) {
  278. // if (res.confirm) {
  279. // wx.openSetting({
  280. // success: (res) => {
  281. // if (res.authSetting["scope.userInfo"]) {////如果用户重新同意了授权登录
  282. // user.loginByWeixin(e.detail).then(res => {
  283. // this.setData({
  284. // userInfo: res.data.userInfo
  285. // });
  286. // app.globalData.userInfo = res.data.userInfo;
  287. // app.globalData.token = res.data.token;
  288. // }).catch((err) => {
  289. // console.log(err)
  290. // });
  291. // }
  292. // }
  293. // })
  294. // }
  295. // }
  296. // });
  297. }
  298. },
  299. exitLogin: function () {
  300. wx.showModal({
  301. title: '',
  302. confirmColor: '#b4282d',
  303. content: '退出登录?',
  304. success: function (res) {
  305. if (res.confirm) {
  306. wx.removeStorageSync('token');
  307. wx.removeStorageSync('userInfo');
  308. wx.switchTab({
  309. url: '/pages/index/index'
  310. });
  311. }
  312. }
  313. })
  314. },
  315. toReport() {
  316. wx.navigateTo({
  317. url: '/dataReport/pages/data-report/data-report',
  318. })
  319. }
  320. })