request.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. import * as config from '@/config'
  2. import WXBizDataCrypt from './WXBizDataCrypt.js'
  3. let baseUrl = config.def().baseUrlNew
  4. // 定义基础请求路径(后端服务器地址)
  5. const baseRequest = (_gp, _mt, data = {}, failCallback) => {
  6. //异步请求数据
  7. return new Promise(resolve => {
  8. // if (!userInfo || !userInfo.accessToken) {
  9. // userInfo = uni.getStorageSync('userInfo')
  10. // }
  11. // let accessToken = userInfo ? userInfo.accessToken : ''
  12. let baseUrl = config.def().baseUrlNew
  13. uni.request({
  14. url: baseUrl + '/m.api',
  15. data: {
  16. ...data,
  17. _gp,
  18. _mt
  19. },
  20. method: 'POST',
  21. header: {
  22. 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
  23. // 'ACCESSTOKEN': accessToken
  24. },
  25. success: (res) => {
  26. if (res.statusCode === 200) {
  27. if (res.data.errno === 200) {
  28. resolve(res.data);
  29. } else {
  30. if (failCallback) {
  31. failCallback(res.data)
  32. } else {
  33. uni.showToast({
  34. title: res.data.errmsg,
  35. icon: 'none'
  36. })
  37. }
  38. }
  39. }
  40. }
  41. })
  42. })
  43. }
  44. //带Token请求
  45. const TokenRequest = (method, url, data, header) => {
  46. var contentheader = 'application/json'
  47. if (header) {
  48. contentheader = header
  49. }
  50. let ac_token = "";
  51. uni.getStorage({
  52. key: 'userInfo',
  53. success: function(res) {
  54. ac_token = res.data.accessToken
  55. }
  56. });
  57. //此token是登录成功后后台返回保存在storage中的
  58. let DefaultOpts = {
  59. url: baseUrl + url,
  60. data: data,
  61. method: method,
  62. header: {
  63. 'content-type': contentheader,
  64. 'Token': ac_token,
  65. }
  66. }
  67. let promise = new Promise(function(resolve, reject) {
  68. uni.request(DefaultOpts).then(
  69. (res) => {
  70. console.log(JSON.stringify(res[1].data))
  71. if (res[1].data.code == '200' || res[1].data.code == 200) {
  72. // 后端返回的状态码100为成功状态,成功则返回请求结果,在app调试时可以通过console.log(JSON.stringify(res[1].data))来查看返回值(以项目实际情况为准)
  73. resolve(res[1].data)
  74. }
  75. if (res[1].data.code == '105' || res[1].data.code == 105) {
  76. // 后端返回状态码为105则为未登录状态(以项目实际情况为准)
  77. uni.showToast({
  78. icon: 'none',
  79. title: '尚未登录',
  80. duration: 2000
  81. });
  82. // 尚未登录的逻辑处理
  83. return false
  84. }
  85. }
  86. ).catch(
  87. (response) => {
  88. reject(response)
  89. }
  90. )
  91. })
  92. return promise
  93. }
  94. //微信登录
  95. const wxlogin = () => {
  96. let promise = new Promise(function(resolve, reject) {
  97. uni.login({
  98. "provider": "weixin",
  99. success: function(wxres) {
  100. console.log("wxlogin",wxres)
  101. baseRequest('commonUserApp', 'commonUserLogin', {
  102. loginType: 1,
  103. raw: JSON.stringify(wxres)
  104. }, failres => {
  105. uni.showToast({
  106. icon: "none",
  107. title: failres.errmsg,
  108. duration: 3000
  109. });
  110. uni.hideLoading()
  111. }).then(res => {
  112. console.log(res.data)
  113. resolve(res.data)
  114. })
  115. },
  116. fail: function(err) {
  117. reject(err.code)
  118. }
  119. })
  120. })
  121. return promise
  122. }
  123. //获取手机号
  124. const getPhone = (e, userInfo) => {
  125. let promise = new Promise(function(resolve, reject) {
  126. if (e.mp.detail.errMsg === "getPhoneNumber:ok") {
  127. let appId = 'wx5d8906c2208c899f'
  128. let sessionKey = userInfo.sessionKey
  129. let encryptedData = e.mp.detail.encryptedData
  130. let iv = e.mp.detail.iv
  131. let data =''
  132. try {
  133. console.log('解密前 encryptedData: ', encryptedData)
  134. console.log('解密前 iv: ', iv)
  135. console.log('解密前 appId: ', appId)
  136. console.log('解密前 sessionKey: ', sessionKey)
  137. data= WXBizDataCrypt.prototype.decryptData(encryptedData, iv, appId, sessionKey)
  138. console.log('解密后 data: ', data)
  139. resolve(data.phoneNumber)
  140. } catch (e) {
  141. uni.showToast({
  142. icon: "none",
  143. title: e,
  144. duration: 3000
  145. });
  146. }
  147. }
  148. })
  149. return promise
  150. }
  151. //同步信息
  152. const syncInfo = (userInfo) => {
  153. let promise = new Promise(function(resolve, reject) {
  154. if (!userInfo.head) userInfo.head =
  155. 'https://taohaoliang.oss-cn-beijing.aliyuncs.com/app/card_head.png'
  156. if (!userInfo.nickname) {
  157. let code = ""
  158. for (var i = 0; i < 6; i++) {
  159. code += parseInt(Math.random() * 10);
  160. }
  161. userInfo.nickname = 'user' + code
  162. }
  163. baseRequest('commonUserApp', 'edit', {
  164. commonUserInfo: JSON.stringify(userInfo)
  165. }, failres => {
  166. uni.showToast({
  167. icon: "none",
  168. title: failres.errmsg,
  169. duration: 3000
  170. });
  171. reject(err.code)
  172. }).then(res => {
  173. resolve(res.data)
  174. })
  175. })
  176. return promise
  177. }
  178. export default {
  179. baseUrl,
  180. baseRequest,
  181. TokenRequest,
  182. wxlogin,
  183. getPhone,
  184. syncInfo
  185. }