index.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. import {
  2. default as storeApi
  3. } from "../../api/store"
  4. import {
  5. default as commonApi
  6. } from "../../api/commonApi"
  7. import {
  8. default as requestApi
  9. } from "../../utils/request"
  10. import {
  11. default as productApi
  12. } from "../../api/product"
  13. const app = getApp();
  14. Page({
  15. data: {
  16. tabs: [],
  17. activeTab: 0,
  18. productHeight: 0,
  19. productList: [],
  20. isLocation: false,
  21. pageNo: 1,
  22. pageSum: 0
  23. },
  24. onLoad: async function (options) {
  25. console.log(options)
  26. const self = this
  27. const tokenData = await commonApi.getToken();
  28. if (tokenData.token && !requestApi.HEAD.token) {
  29. console.log('设置token')
  30. wx.setStorageSync('token', tokenData.token)
  31. requestApi.HEAD.token = tokenData.token
  32. }
  33. if (!options.storeId){
  34. wx.getSetting({
  35. success: (res) => {
  36. let authSetting = res.authSetting
  37. if (authSetting['scope.userLocation']) {
  38. console.log('已授权地理位置')
  39. // 已授权
  40. self.getLocation();
  41. } else {
  42. // 未授权
  43. console.log('未授权地理位置');
  44. wx.getLocation({
  45. type: 'wgs84',
  46. async success(res) {
  47. console.log(res)
  48. const latitude = res.latitude
  49. const longitude = res.longitude
  50. wx.setStorageSync('lon', longitude)
  51. wx.setStorageSync('lat', latitude)
  52. const rs = await storeApi.getNearStore(latitude, longitude)
  53. console.log(rs.info);
  54. self.setData({
  55. locationFlag: true,
  56. storeInfo: rs.info
  57. })
  58. }
  59. })
  60. }
  61. }
  62. })
  63. }else {
  64. const rs = await storeApi.getStoreByI(options.storeId)
  65. self.setData({
  66. storeInfo: rs.info
  67. })
  68. }
  69. // this.getProductHeight()
  70. },
  71. onShow: async function (op) {
  72. console.log(op)
  73. const self = this
  74. const tokenData = await commonApi.getToken();
  75. if (tokenData.token && !requestApi.HEAD.token) {
  76. wx.setStorageSync('token', tokenData.token)
  77. requestApi.HEAD.token = tokenData.token
  78. }
  79. if (self.data.chooseStoreId){
  80. const rs = await storeApi.getStoreById(self.data.chooseStoreId)
  81. self.setData({
  82. storeInfo: rs.info,
  83. chooseStoreId:0
  84. })
  85. }
  86. var tabs = await this.getProductList(true);
  87. this.setData({
  88. tabs
  89. });
  90. },
  91. getProductList: async function (refresh = false) {
  92. const self = this
  93. let pageNo = 1
  94. if (refresh) {
  95. pageNo = 1
  96. this.setData({
  97. pageNo
  98. })
  99. } else {
  100. pageNo = this.data.pageNo;
  101. }
  102. let productList = this.data.productList;
  103. const proList = await productApi.getProductList(pageNo, 4)
  104. let list = proList.list;
  105. if (pageNo == 1) {
  106. productList = list
  107. } else {
  108. list.forEach((item) => {
  109. productList.push(item)
  110. })
  111. }
  112. self.setData({
  113. productList,
  114. pageSum: proList.pageCount
  115. })
  116. },
  117. requestAll(url, data, header = {}, method) {
  118. const _self = this
  119. wx.showLoading()
  120. return new Promise((resolve, reject) => {
  121. wx.request({
  122. url: url,
  123. data: data,
  124. header: header,
  125. dataType: 'json',
  126. method: method,
  127. success: (res => {
  128. wx.hideLoading()
  129. if (res.data.code === 1) {
  130. //200: 服务端业务处理正常结束
  131. resolve(res.data)
  132. } else {
  133. if (res.data.code === 0) {
  134. wx.showToast({
  135. title: res.data.message,
  136. })
  137. }
  138. if (res.data.code === 901) {
  139. console.log(res.data)
  140. }
  141. //其它错误,提示用户错误信息
  142. if (this._errorHandler != null) {
  143. //如果有统一的异常处理,就先调用统一异常处理函数对异常进行处理
  144. this._errorHandler(res)
  145. }
  146. reject(res)
  147. }
  148. }),
  149. fail: (res => {
  150. if (this._errorHandler != null) {
  151. this._errorHandler(res)
  152. }
  153. wx.showToast({
  154. title: '网络异常请,稍后再试~',
  155. })
  156. reject(res)
  157. })
  158. })
  159. })
  160. },
  161. onTabClick(e) {
  162. const index = e.detail.index
  163. this.setData({
  164. activeTab: index
  165. })
  166. },
  167. onChange(e) {
  168. const index = e.detail.index
  169. this.setData({
  170. activeTab: index
  171. })
  172. },
  173. handleClick(e) {
  174. },
  175. getLocation: async function () {
  176. console.log('getlocation')
  177. const self = this
  178. wx.getLocation({
  179. type: 'wgs84',
  180. async success(res) {
  181. console.log(res)
  182. const latitude = res.latitude
  183. const longitude = res.longitude
  184. wx.setStorageSync('lon', longitude)
  185. wx.setStorageSync('lat', latitude)
  186. const speed = res.speed
  187. const accuracy = res.accuracy
  188. const rs = await storeApi.getNearStore(latitude, longitude)
  189. console.log(rs.info);
  190. self.setData({
  191. locationFlag: true,
  192. storeInfo: rs.info
  193. })
  194. },
  195. fail(err) {
  196. console.log(err)
  197. }
  198. })
  199. },
  200. /**
  201. * 页面上拉触底事件的处理函数
  202. */
  203. onReachBottom: function () {
  204. console.log(1213131)
  205. let pageNo = this.data.pageNo;
  206. let pageSum = this.data.pageSum;
  207. pageNo++;
  208. if (this.data.pageNo <= pageSum) {
  209. this.setData({
  210. pageNo
  211. })
  212. this.getProductList()
  213. }
  214. },
  215. /**
  216. * 页面相关事件处理函数--监听用户下拉动作
  217. */
  218. onPullDownRefresh: function () {
  219. console.log(111)
  220. },
  221. showProduct: async function (e) {
  222. const self = this
  223. console.log(e.currentTarget.dataset.productid)
  224. var productId = e.currentTarget.dataset.productid;
  225. if (this.data.storeInfo) {
  226. var shopId = this.data.storeInfo.id;
  227. wx.navigateTo({
  228. url: '../product/product?shopId=' + shopId + '&productId=' + productId,
  229. })
  230. return
  231. }
  232. wx.getLocation({
  233. type: 'wgs84',
  234. async success(res) {
  235. console.log(res)
  236. const latitude = res.latitude
  237. const longitude = res.longitude
  238. const speed = res.speed
  239. const accuracy = res.accuracy
  240. const rs = await storeApi.getNearStore(latitude, longitude)
  241. console.log(rs.info);
  242. self.setData({
  243. locationFlag: true,
  244. storeInfo: rs.info
  245. })
  246. var shopId = self.data.storeInfo.id;
  247. wx.navigateTo({
  248. url: '../product/product?shopId=' + shopId + '&productId=' + productId,
  249. })
  250. }
  251. })
  252. },
  253. gotoAppointment: async function (e) {
  254. const self = this
  255. console.log(e.currentTarget.dataset.productid)
  256. let productId = e.currentTarget.dataset.productid;
  257. let shopId = this.data.storeInfo.id;
  258. let address = self.data.storeInfo.address;
  259. let shopName = self.data.storeInfo.storeName;
  260. const isAuth = await app.isAuth()
  261. if (!isAuth) {
  262. // wx.redirectTo({
  263. // url: '/pages/prompt/prompt?page=' + this.route + `&shopId=${shopId}&productId=${productId}&address=${address}&shopName=${shopName}`,
  264. // })
  265. // return
  266. }
  267. wx.navigateTo({
  268. url: '../appointment/appointment?' + 'shopId=' + shopId + '&productId=' + productId + '&address=' + address+"&shopName="+shopName,
  269. })
  270. }
  271. })