add-step1.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. var d = dialog({contentType:'load', skin:'bk-popup'});
  2. var dd = dialog({contentType:'load', skin:'bk-popup', content:'提交中...'});
  3. var relation;
  4. var family = [];
  5. $(function(){
  6. // 判断是否登录
  7. checkUserAgent();
  8. getFamilyMembersPromise()
  9. .then(function(res) {
  10. if(res.status == 200) {
  11. family = res.data.normalmembers;
  12. } else {
  13. dialog({contentType:'tipsbox', skin:'bk-popup' , content: res.msg||'数据获取失败'}).show();
  14. }
  15. })
  16. .catch(function(e) {
  17. console && console.error(e)
  18. })
  19. // 选择家庭关系
  20. $('#select').mobiscroll({
  21. theme: 'ios',
  22. lang: 'zh',
  23. formatValue: function(d) {
  24. return d;
  25. },
  26. customWheels: true,
  27. wheels: [
  28. [{
  29. keys: ['1', '2', '3', '4', '5', '6', '0'],
  30. values: ['父亲', '母亲', '老公', '老婆', '儿子', '女儿', '其他']
  31. }]
  32. ],
  33. onSelect: function(valueText, inst) {
  34. var dd = eval("[" + valueText + "]");
  35. $('#select').val(dd[0].values);
  36. relation = dd[0].keys;
  37. }
  38. });
  39. // 下一步
  40. $("#next").bind("tap", function () {
  41. if(!$(this).hasClass('btn-disabled')) {
  42. commit();
  43. }
  44. });
  45. });
  46. function checkVal ( cv, attr, d) {
  47. var len = d.length,
  48. sta = 1;
  49. for ( var i = 0; i < len; i++) {
  50. if (d[i][attr] == cv) {
  51. sta = -1;
  52. break;
  53. }
  54. }
  55. return sta;
  56. }
  57. // 禁止拖动
  58. document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false);
  59. function getFamilyMembersPromise() {
  60. d.show();
  61. return new Promise(function(resolve, reject){
  62. sendPost("patient/family/members", {}, "json", "get", function fail() {
  63. d.close();
  64. dialog({contentType:'tipsbox', skin:'bk-popup' , content:'数据获取失败'}).show();
  65. }, function success(res) {
  66. d.close();
  67. resolve(res)
  68. });
  69. })
  70. }
  71. //验证信息
  72. function validate(data) {
  73. if (data.name.trim() == "") {
  74. dialog({contentType:'tipsbox', skin:'bk-popup' , content:'请输入姓名'}).show();
  75. return false;
  76. }
  77. if (!isIdcard(data.idcard)) {
  78. dialog({contentType:'tipsbox', skin:'bk-popup' , content:'身份证格式不正确'}).show();
  79. return false;
  80. }
  81. if (data.select.trim() == "") {
  82. dialog({contentType:'tipsbox', skin:'bk-popup' , content:'请选择家庭关系'}).show();
  83. return false;
  84. }
  85. if (checkVal( data.name, 'name', family) == -1 || checkVal( data.idcard, 'idcard', family) == -1) {
  86. dialog({contentType:'tipsbox', skin:'bk-popup' , content: data.name + '已是您的家庭成员咯~'}).show();
  87. return false;
  88. }
  89. return true;
  90. }
  91. // 提交失败
  92. function commitFailed (res) {
  93. dd.close();
  94. if (res && res.msg) {
  95. var d1 = dialog({contentType:'tipsbox', skin:'bk-popup' , content:res.msg}).show();
  96. } else {
  97. var d2 = dialog({contentType:'tipsbox', skin:'bk-popup' , content:'加载失败'}).show();
  98. }
  99. }
  100. function checkMemberPromise(code) {
  101. return new Promise(function(resolve, reject) {
  102. sendPost("patient/family/check_member", {
  103. memberCode: code
  104. }, "json", "get", commitFailed, function(res) {
  105. resolve(res)
  106. });
  107. })
  108. }
  109. function sendWXTempPromise(data) {
  110. return new Promise(function(resolve, reject) {
  111. sendPost("patient/family/sendWXTemp", data , "json", "POST", commitFailed, function(res) {
  112. resolve(res)
  113. });
  114. })
  115. }
  116. // 提交成功
  117. function commitSuccess (res) {
  118. if (res.status == 200) {
  119. var data = res.data;
  120. if (data.isRegister == -1) {
  121. dd.close();
  122. $('#next').removeClass("btn-disabled");
  123. var d4 = dialog({contentType:'tipsbox', skin:'bk-popup' , content:'别闹了,这是您自己,快添加您的家人吧'}).show();
  124. } else if (data.isRegister == 0) {
  125. dd.close();
  126. $('#next').removeClass("btn-disabled");
  127. dialog({
  128. content:'对不起,目前仅支持添加注册用户为家人哦,是否推荐家人注册?',
  129. okValue: '前往推荐',
  130. ok: function (){
  131. // 跳转分享
  132. window.location.href = '../html/family-share.html';
  133. },
  134. cancel: function () {}
  135. }).showModal();
  136. } else {
  137. if (data.name != $("#name").val()) {
  138. dd.close();
  139. $('#next').removeClass("btn-disabled");
  140. var d4 = dialog({contentType:'tipsbox', skin:'bk-popup' , content:'姓名与身份证不一致'}).show();
  141. } else {
  142. window.localStorage.removeItem("familyCode");
  143. window.localStorage.setItem("familyCode", data.code);
  144. window.localStorage.removeItem("familyName");
  145. window.localStorage.setItem("familyName", data.name);
  146. window.localStorage.removeItem("familyImgUrl");
  147. window.localStorage.setItem("familyImgUrl", getImgUrl(data.photo));
  148. window.localStorage.removeItem("familyRelation");
  149. window.localStorage.setItem("familyRelation", relation);
  150. checkMemberPromise(data.code)
  151. .then(function(res) {
  152. dd.close();
  153. if(res.status == "200") {
  154. var state = res.data.state;
  155. if(state == "0") { // 未绑定微信和手机
  156. $('#next').removeClass("btn-disabled");
  157. window.location.href = 'add-step3.html';
  158. } else if(state == "1") { // 仅绑定微信
  159. sendWXTempPromise({
  160. member: data.code,
  161. relation: relation
  162. })
  163. .then(function(res) {
  164. if(res.status=="200") {
  165. $('#next').addClass("btn-disabled");
  166. dialog({contentType:'tipsbox', skin:'bk-popup' , content:"已发送添加申请"}).show();
  167. } else {
  168. $('#next').removeClass("btn-disabled");
  169. if(res.msg){
  170. dialog({contentType:'tipsbox', skin:'bk-popup' , content:res.msg}).show();
  171. }else
  172. dialog({contentType:'tipsbox', skin:'bk-popup' , content:"发送失败"}).show();
  173. }
  174. })
  175. } else if(state == "2") { // 仅绑定手机
  176. $('#next').removeClass("btn-disabled");
  177. window.localStorage.removeItem("familyMobile");
  178. window.localStorage.setItem("familyMobile", data.mobile);
  179. window.location.href = 'add-step2.html';
  180. } else if(state== "3") { // 手机和微信都绑定
  181. $('#next').removeClass("btn-disabled");
  182. window.localStorage.removeItem("familyMobile");
  183. window.localStorage.setItem("familyMobile", data.mobile);
  184. window.location.href = 'add-step2-pre.html';
  185. }
  186. } else {
  187. dd.close();
  188. dialog({contentType:'tipsbox', skin:'bk-popup' , content:'加载失败'}).show()
  189. }
  190. })
  191. .catch(function(e) {
  192. dd.close();
  193. console && console.error(e)
  194. })
  195. // if (data.mobile == "") {
  196. // window.location.href = 'add-step3.html';
  197. // } else {
  198. // window.localStorage.removeItem("familyMobile");
  199. // window.localStorage.setItem("familyMobile", data.mobile);
  200. // window.location.href = 'add-step2.html';
  201. // }
  202. }
  203. }
  204. } else {
  205. //非200则为失败
  206. commitFailed(res);
  207. }
  208. }
  209. // 提交添加家人
  210. function commit() {
  211. var data = {};
  212. data.name = $("#name").val();
  213. data.idcard = $("#idcard").val();
  214. data.select = $("#select").val();
  215. if (validate(data)) {
  216. // 验证通过执行
  217. dd.show();
  218. $('#next').addClass("btn-disabled");
  219. sendPost("patient/family/is_register", data, "json", "get", commitFailed, commitSuccess);
  220. }
  221. }