card.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. //index.js
  2. //获取应用实例
  3. const app = getApp()
  4. import WxValidate from '../../utils/wxValidate';
  5. Page({
  6. data: {
  7. form: {},
  8. // 省份
  9. provinceList: [],
  10. // 市区
  11. cityList: [],
  12. // 套餐列表
  13. setList: [],
  14. },
  15. initValidate() {
  16. const rules = {
  17. mobile: { required: true, tel: true },
  18. password: { required: true, },
  19. province: { required: true, },
  20. city: { required: false, },
  21. set: { required: true, },
  22. name: { required: true, },
  23. id_card: { required: true, idcard: true },
  24. wxaccount: { required: true, },
  25. }
  26. // 验证字段的提示信息,若不传则调用默认的信息
  27. const messages = {
  28. mobile: { required: '请输入新办理号码', },
  29. password: { required: '请输入密码', },
  30. province: { required: '请选择省份', },
  31. city: { required: '请选择市区', },
  32. set: { required: '请选择套餐', },
  33. name: { required: '请输入姓名', },
  34. id_card: { required: '请输入身份证号', },
  35. wxaccount: { required: '请输入微信账号', },
  36. };
  37. this.WxValidate = new WxValidate(rules, messages)
  38. },
  39. // 选择省份
  40. provinceChange: function (e) {
  41. this.setData({ "form.province": this.data.provinceList[e.detail.value].code })
  42. this.setData({ "form.provinces": this.data.provinceList[e.detail.value].name })
  43. // 查询市
  44. wx.request({
  45. url: app.globalData.publicUrl + '/api/htyd/xzqh',
  46. method: "get",
  47. data: { pcode: this.data.provinceList[e.detail.value].code },
  48. success: (res) => {
  49. if (res.data.errcode == '0') {
  50. this.setData({ cityList: res.data.data })
  51. // 根据查询市,判断查询套餐
  52. if (res.data.total === 0) {
  53. // 套餐
  54. wx.request({
  55. url: app.globalData.publicUrl + '/api/htyd/set',
  56. method: "get",
  57. data: { contact: this.data.provinceList[e.detail.value].code },
  58. success: (res) => {
  59. if (res.data.errcode == '0') {
  60. this.setData({ setList: res.data.data })
  61. } else {
  62. wx.showToast({
  63. title: '查询失败',
  64. icon: 'error',
  65. duration: 2000
  66. })
  67. }
  68. }
  69. })
  70. }
  71. } else {
  72. wx.showToast({
  73. title: '查询失败',
  74. icon: 'error',
  75. duration: 2000
  76. })
  77. }
  78. }
  79. })
  80. },
  81. // 选择市区
  82. cityChange: function (e) {
  83. this.setData({ "form.city": this.data.cityList[e.detail.value].code })
  84. this.setData({ "form.citys": this.data.cityList[e.detail.value].name })
  85. // 套餐
  86. wx.request({
  87. url: app.globalData.publicUrl + '/api/htyd/set',
  88. method: "get",
  89. data: { contact: this.data.cityList[e.detail.value].code },
  90. success: (res) => {
  91. if (res.data.errcode == '0') {
  92. this.setData({ setList: res.data.data })
  93. } else {
  94. wx.showToast({
  95. title: '查询失败',
  96. icon: 'error',
  97. duration: 2000
  98. })
  99. }
  100. }
  101. })
  102. },
  103. // 选择套餐
  104. setChange: function (e) {
  105. this.setData({ "form.set": this.data.setList[e.detail.value].id })
  106. this.setData({ "form.sets": this.data.setList[e.detail.value].title })
  107. },
  108. //事件处理函数
  109. formSubmit: function (e) {
  110. const params = e.detail.value;
  111. if (!this.WxValidate.checkForm(params)) {
  112. const error = this.WxValidate.errorList[0];
  113. wx.showToast({
  114. title: error.msg,
  115. icon: 'none',
  116. duration: 2000
  117. })
  118. return false
  119. } else {
  120. console.log(params);
  121. }
  122. wx.request({
  123. url: app.globalData.publicUrl + '/api/htyd/card',
  124. method: "post",
  125. data: params,
  126. success: (res) => {
  127. if (res.data.errcode == '0') {
  128. wx.showToast({
  129. title: '办卡成功',
  130. icon: 'success',
  131. duration: 2000
  132. })
  133. wx.redirectTo({
  134. url: '/pages/home/home',
  135. })
  136. } else {
  137. wx.showToast({
  138. title: '办卡失败',
  139. icon: 'error',
  140. duration: 2000
  141. })
  142. }
  143. }
  144. })
  145. },
  146. onLoad: function (options) {
  147. this.initValidate()//验证规则函数
  148. // 获取推荐人信息
  149. let data = app.globalData.userInfo;
  150. if (data) {
  151. console.log('function in if');
  152. this.setData({ "form.recommend": data.name })
  153. this.setData({ "form.r_mobile": data.mobile })
  154. } else {
  155. let qrUrl = decodeURIComponent(options.q)
  156. let mobile = this.getQueryString(qrUrl, 'mobile');
  157. let name = this.getQueryString(qrUrl, 'name');
  158. console.log('function in else');
  159. console.log(`mobile:${mobile}`);
  160. console.log(`name:${name}`);
  161. if (name && mobile) {
  162. this.setData({ "form.recommend": name })
  163. this.setData({ "form.r_mobile": mobile })
  164. }
  165. }
  166. // 查询省
  167. wx.request({
  168. url: app.globalData.publicUrl + '/api/htyd/xzqh',
  169. method: "get",
  170. data: {},
  171. success: (res) => {
  172. if (res.data.errcode == '0') {
  173. this.setData({ provinceList: res.data.data })
  174. } else {
  175. wx.showToast({
  176. title: '查询失败',
  177. icon: 'error',
  178. duration: 2000
  179. })
  180. }
  181. }
  182. })
  183. },
  184. getQueryString: function (url, name) {
  185. const query = url.split('?')[1];
  186. const objs = query.split('&');
  187. let res;
  188. for (const obj of objs) {
  189. const kv = obj.split('=');
  190. if (name == kv[0]) res = kv[1];
  191. }
  192. return res;
  193. }
  194. })