weui-util.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. var loading = null;
  2. function showToast(message,delay){
  3. weui.toast(message,delay||3000);
  4. }
  5. function showLoading(message,delay){
  6. if(loading) return;
  7. loading = weui.loading(message, {
  8. className: 'custom-classname'
  9. });
  10. if(delay){
  11. setTimeout(function () {
  12. hideLoading();
  13. }, delay||3000);
  14. }
  15. }
  16. function hideLoading(){
  17. if(loading) loading.hide();
  18. loading = null;
  19. }
  20. function showAlert(message,title, onClick, retry){
  21. if($('.weui-dialog').length > 0){
  22. retry = retry || 0;
  23. if(retry && retry > 30){
  24. weui.topTips('不能同时显示多个对话框');
  25. return;
  26. }
  27. setTimeout(function(){
  28. showAlert(message,title, onClick, ++retry);
  29. },100);
  30. }else{
  31. weui.alert(message, onClick, { title: title, isAndroid: false });
  32. }
  33. }
  34. function showConfirm(message,title,callback){
  35. weui.confirm(message, function (){
  36. if(callback && callback instanceof Function)
  37. callback(true);
  38. },function(){
  39. if(callback && callback instanceof Function)
  40. callback(false);
  41. },{ title: title, isAndroid: false });
  42. }
  43. function onConfirm(res){
  44. if(res)
  45. alert('click yes');
  46. else
  47. alert('click no');
  48. }
  49. function showCustom1(message,title){
  50. promptVC({
  51. text: message,
  52. title: title,
  53. onOK: function(val){
  54. //val格式: {verifyCode: 'xxxxxx'}
  55. console.log(val)
  56. alert(JSON.stringify(val))
  57. },
  58. onSend: function(){
  59. showToast("验证码已发送")
  60. },
  61. isAndroid: false
  62. });
  63. }
  64. function showCustom2(message,title){
  65. promptVC({
  66. text: message,
  67. title: title,
  68. inputPhone: true,
  69. onOK: function(val){
  70. //val格式: {phone: '13xxxxxxxx', verifyCode: 'xxxxxx'}
  71. console.log(val)
  72. // if(!/1[3-8]\d{9}/.test(val.phone)){
  73. // weui.topTips('手机号无效');
  74. // return false;
  75. // }
  76. alert(JSON.stringify(val))
  77. },
  78. onSend: function(val){
  79. if(/1[3-8]\d{9}/.test(val))
  80. showToast("验证码已发送")
  81. else{
  82. weui.topTips('手机号无效');
  83. return false;
  84. }
  85. }
  86. });
  87. }
  88. function promptOrder(message, pkgName, pkgCode, okText, cancelText, onSubmit, onCancel){
  89. var imgUrl = 'verifyImage?timestamp=' + new Date().getTime();
  90. promptVC({
  91. text: message,
  92. title: pkgName,
  93. inputPhone: true,
  94. okText: okText || "办理",
  95. cancelText: cancelText || "关闭",
  96. verifyImg: imgUrl,
  97. isAndroid: false,
  98. onOK: function(val){
  99. //val格式: {phone: '13xxxxxxxx', verifyCode: 'xxxxxx', verifyImg: 'xxxx'}
  100. console.log(val)
  101. if(onSubmit)
  102. onSubmit(val.phone, val.verifyCode, val.verifyImg, pkgCode, pkgName);
  103. },
  104. onCancel: onCancel,
  105. onSend: function(phone){
  106. requestVerify(phone);
  107. }
  108. });
  109. $('img.weui-prompt-button').click(function(){
  110. $(this).attr('src','verifyImage?timestamp=' + new Date().getTime());
  111. });
  112. }
  113. function promptAction(message, title, okText, cancelText, onSubmit){
  114. var imgUrl = 'verifyImage?timestamp=' + new Date().getTime();
  115. var opts = {};
  116. if(message instanceof Object){
  117. opts = message;
  118. }else{
  119. opts.message = message;
  120. opts.title = title;
  121. opts.okText = okText;
  122. opts.cancelText = cancelText;
  123. opts.onSubmit = onSubmit;
  124. }
  125. if(opts.imgUrl == undefined)
  126. opts.imgUrl = imgUrl;
  127. promptVC({
  128. text: opts.message,
  129. title: opts.title,
  130. inputPhone: opts.phoneNo || true,
  131. okText: opts.okText || "办理",
  132. cancelText: opts.cancelText || "关闭",
  133. verifyImg: opts.imgUrl,
  134. isAndroid: false,
  135. onOK: function(val){
  136. //val格式: {phone: '13xxxxxxxx', verifyCode: 'xxxxxx', verifyImg: 'xxxx'}
  137. console.log(val)
  138. if(opts.onSubmit && opts.onSubmit instanceof Function){
  139. opts.onSubmit(val.phone, val.verifyCode, val.verifyImg);
  140. }
  141. },
  142. onCancel: function() {
  143. if(opts.closable === false)
  144. return false;
  145. },
  146. onSend: function(phone){
  147. requestVerify(phone, opts.verifyCode);
  148. }
  149. });
  150. $('img.weui-prompt-button').click(function(){
  151. $(this).attr('src','verifyImage?timestamp=' + new Date().getTime());
  152. });
  153. }
  154. function requestVerify(phone, opts) {
  155. showLoading("请求发送中");
  156. opts = opts || {};
  157. var params = {};
  158. params[opts.param || 'phoneNo'] = phone;
  159. $.ajax({
  160. type : "post",
  161. url : opts.url || 'verifyCode',
  162. data : params,
  163. dataType : 'json',
  164. success : function(result) {
  165. hideLoading();
  166. if (result.status == 0) {
  167. showToast("验证码已发送,请查收短信", 3000);
  168. } else {
  169. console.log(result);
  170. weui.topTips(result.message);
  171. }
  172. }
  173. }).always(function() {
  174. hideLoading();
  175. });
  176. }
  177. //输入验证码
  178. // options = {
  179. // message: String,
  180. // title: String,
  181. // phoneNo: String 或 true
  182. // okText: String,
  183. // cancelText: String,
  184. // onResult: Function,
  185. // verifyImg: Boolean,
  186. // closable: Boolean,
  187. // }
  188. function showLogin(message, title, okText, cancelText, onResult){
  189. var opts = {};
  190. if(message instanceof Object){
  191. opts = message;
  192. }else{
  193. opts.message = message;
  194. opts.title = title;
  195. opts.okText = okText;
  196. opts.cancelText = cancelText;
  197. opts.onResult = onResult;
  198. }
  199. if(!opts.onResult || !(opts.onResult instanceof Function)){
  200. showAlert("调用错误:必须指定onResult回调参数");
  201. return;
  202. }
  203. var imgUrl = 'verifyImage?timestamp=' + new Date().getTime();
  204. if(opts.verifyImg)
  205. opts.imgUrl = imgUrl;
  206. promptVC({
  207. text: opts.message,
  208. title: opts.title,
  209. inputPhone: opts.phoneNo || true,
  210. okText: opts.okText || "登录",
  211. cancelText: opts.cancelText || "关闭",
  212. verifyImg: opts.imgUrl,
  213. isAndroid: false,
  214. onOK: function(val){
  215. //val格式: {phone: '13xxxxxxxx', verifyCode: 'xxxxxx', verifyImg: 'xxxx'}
  216. console.log(val)
  217. var param = {
  218. phoneNo : val.phone,
  219. verifyCode : val.verifyCode,
  220. verifyImg : val.verifyImg
  221. };
  222. showLoading();
  223. $.post('login', param)
  224. .then(function(result) {
  225. opts.onResult(result);
  226. }).fail(function( jqXHR, textStatus, errorThrown ) {
  227. var msg = "请求发送失败";
  228. if(typeof jqXHR == "string")
  229. msg = jqXHR;
  230. weui.topTips(msg);
  231. }).always(function(){
  232. hideLoading();
  233. });
  234. },
  235. onCancel: function() {
  236. if(opts.closable === false)
  237. return false;
  238. },
  239. onSend: function(phone){
  240. requestVerify(phone, opts.verifyCode);
  241. }
  242. });
  243. $('img.weui-prompt-button').click(function(){
  244. $(this).attr('src','verifyImage?timestamp=' + new Date().getTime());
  245. });
  246. }