auth.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. const WXAPI = require('apifm-wxapi')
  2. import Dialog from '@vant/weapp/dialog/dialog'
  3. async function checkSession(){
  4. return new Promise((resolve, reject) => {
  5. wx.checkSession({
  6. success() {
  7. return resolve(true)
  8. },
  9. fail() {
  10. return resolve(false)
  11. }
  12. })
  13. })
  14. }
  15. async function bindSeller() {
  16. const token = wx.getStorageSync('token')
  17. const referrer = wx.getStorageSync('referrer')
  18. if (!token) {
  19. return
  20. }
  21. if (!referrer) {
  22. return
  23. }
  24. const res = await WXAPI.bindSeller({
  25. token,
  26. uid: referrer
  27. })
  28. }
  29. // 检测登录状态,返回 true / false
  30. async function checkHasLogined() {
  31. const token = wx.getStorageSync('token')
  32. if (!token) {
  33. return false
  34. }
  35. const loggined = await checkSession()
  36. if (!loggined) {
  37. wx.removeStorageSync('token')
  38. return false
  39. }
  40. const checkTokenRes = await WXAPI.checkToken(token)
  41. if (checkTokenRes.code != 0) {
  42. wx.removeStorageSync('token')
  43. return false
  44. }
  45. return true
  46. }
  47. async function wxaCode(){
  48. return new Promise((resolve, reject) => {
  49. wx.login({
  50. success(res) {
  51. return resolve(res.code)
  52. },
  53. fail() {
  54. wx.showToast({
  55. title: '获取code失败',
  56. icon: 'none'
  57. })
  58. return resolve('获取code失败')
  59. }
  60. })
  61. })
  62. }
  63. async function login(page){
  64. const _this = this
  65. wx.login({
  66. success: function (res) {
  67. const extConfigSync = wx.getExtConfigSync()
  68. if (extConfigSync.subDomain) {
  69. WXAPI.wxappServiceLogin({
  70. code: res.code
  71. }).then(function (res) {
  72. if (res.code == 10000) {
  73. // 去注册
  74. return;
  75. }
  76. if (res.code != 0) {
  77. // 登录错误
  78. wx.showModal({
  79. title: '无法登录',
  80. content: res.msg,
  81. showCancel: false
  82. })
  83. return;
  84. }
  85. wx.setStorageSync('token', res.data.token)
  86. wx.setStorageSync('uid', res.data.uid)
  87. _this.bindSeller()
  88. if ( page ) {
  89. page.onShow()
  90. }
  91. })
  92. } else {
  93. WXAPI.login_wx(res.code).then(function (res) {
  94. if (res.code == 10000) {
  95. // 去注册
  96. return;
  97. }
  98. if (res.code != 0) {
  99. // 登录错误
  100. wx.showModal({
  101. title: '无法登录',
  102. content: res.msg,
  103. showCancel: false
  104. })
  105. return;
  106. }
  107. wx.setStorageSync('token', res.data.token)
  108. wx.setStorageSync('uid', res.data.uid)
  109. _this.bindSeller()
  110. if ( page ) {
  111. page.onShow()
  112. }
  113. })
  114. }
  115. }
  116. })
  117. }
  118. async function authorize() {
  119. // const code = await wxaCode()
  120. // const resLogin = await WXAPI.login_wx(code)
  121. // if (resLogin.code == 0) {
  122. // wx.setStorageSync('token', resLogin.data.token)
  123. // wx.setStorageSync('uid', resLogin.data.uid)
  124. // return resLogin
  125. // }
  126. return new Promise((resolve, reject) => {
  127. wx.login({
  128. success: function (res) {
  129. const code = res.code
  130. // console.log("!!!"+code)
  131. let referrer = '' // 推荐人
  132. let referrer_storge = wx.getStorageSync('referrer');
  133. if (referrer_storge) {
  134. referrer = referrer_storge;
  135. }
  136. // 下面开始调用注册接口
  137. const extConfigSync = wx.getExtConfigSync()
  138. // console.log(extConfigSync.subDomain)
  139. if (extConfigSync.subDomain) {
  140. WXAPI.wxappServiceAuthorize({
  141. code: code,
  142. referrer: referrer
  143. }).then(function (res) {
  144. if (res.code == 0) {
  145. wx.setStorageSync('token', res.data.token)
  146. wx.setStorageSync('uid', res.data.uid)
  147. resolve(res)
  148. } else {
  149. wx.showToast({
  150. title: res.msg,
  151. icon: 'none'
  152. })
  153. reject(res.msg)
  154. }
  155. })
  156. } else {
  157. WXAPI.authorize({
  158. code: code,
  159. referrer: referrer
  160. }).then(function (res) {
  161. // console.log(res.code)
  162. if (res.code == 0) {
  163. wx.setStorageSync('token', res.data.token)
  164. wx.setStorageSync('uid', res.data.uid)
  165. resolve(res)
  166. } else {
  167. wx.showToast({
  168. title: res.msg,
  169. icon: 'none'
  170. })
  171. reject(res.msg)
  172. }
  173. })
  174. }
  175. },
  176. fail: err => {
  177. reject(err)
  178. }
  179. })
  180. })
  181. }
  182. function loginOut(){
  183. wx.removeStorageSync('token')
  184. wx.removeStorageSync('uid')
  185. }
  186. async function checkAndAuthorize (scope) {
  187. return new Promise((resolve, reject) => {
  188. wx.getSetting({
  189. success(res) {
  190. if (!res.authSetting[scope]) {
  191. wx.authorize({
  192. scope: scope,
  193. success() {
  194. resolve() // 无返回参数
  195. },
  196. fail(e){
  197. console.error(e)
  198. // if (e.errMsg.indexof('auth deny') != -1) {
  199. // wx.showToast({
  200. // title: e.errMsg,
  201. // icon: 'none'
  202. // })
  203. // }
  204. wx.showModal({
  205. title: '无权操作',
  206. content: '需要获得您的授权',
  207. showCancel: false,
  208. confirmText: '立即授权',
  209. confirmColor: '#e64340',
  210. success(res) {
  211. wx.openSetting();
  212. },
  213. fail(e){
  214. console.error(e)
  215. reject(e)
  216. },
  217. })
  218. }
  219. })
  220. } else {
  221. resolve() // 无返回参数
  222. }
  223. },
  224. fail(e){
  225. console.error(e)
  226. reject(e)
  227. }
  228. })
  229. })
  230. }
  231. module.exports = {
  232. checkHasLogined: checkHasLogined,
  233. wxaCode: wxaCode,
  234. login: login,
  235. loginOut: loginOut,
  236. checkAndAuthorize: checkAndAuthorize,
  237. authorize: authorize,
  238. bindSeller: bindSeller
  239. }