config.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
  2. var Controller = {
  3. index: () => {
  4. const { reactive, onMounted, ref } = Vue
  5. const index = {
  6. setup() {
  7. const state = reactive({
  8. receiver_type: 'user',
  9. data: [],
  10. qrcodeUrl: '',
  11. eventId: '',
  12. scanStatus: '',
  13. oauthInfo: {},
  14. })
  15. function getData() {
  16. Fast.api.ajax({
  17. url: 'shopro/notification/config',
  18. type: 'GET',
  19. data: {
  20. receiver_type: state.receiver_type,
  21. },
  22. }, function (ret, res) {
  23. state.data = res.data
  24. return false
  25. }, function (ret, res) { })
  26. }
  27. function onEdit(event, channel) {
  28. Fast.api.open(`shopro/notification/config/edit?event=${event}&channel=${channel}`, "编辑", {
  29. callback() {
  30. getData()
  31. }
  32. })
  33. }
  34. function onSetStatus(status, event, channel) {
  35. Fast.api.ajax({
  36. url: `shopro/notification/config/setStatus/id/${event}`,
  37. type: 'POST',
  38. data: {
  39. status, event, channel
  40. }
  41. }, function (ret, res) {
  42. getData()
  43. }, function (ret, res) { })
  44. }
  45. // 点击二维码
  46. function onQrcode() {
  47. state.qrcodeUrl = '';
  48. getQrcode();
  49. }
  50. function onHideQrcode() {
  51. state.eventId = '';
  52. }
  53. // 获取绑定二维码
  54. function getQrcode() {
  55. Fast.api.ajax({
  56. url: 'shopro/wechat/admin/getQrcode?event=bind',
  57. type: 'GET',
  58. loading: false,
  59. }, function (ret, res) {
  60. if (res.code === 1) {
  61. state.qrcodeUrl = ret.url;
  62. state.eventId = ret.eventId;
  63. // 待扫码
  64. state.scanStatus = 'pending';
  65. checkScanResult(ret.eventId);
  66. }
  67. return false;
  68. }, function (data, res) {
  69. if (res.code === -2) {
  70. // 已绑定
  71. state.scanStatus = 'binded';
  72. state.oauthInfo = res.data;
  73. }
  74. return false;
  75. })
  76. }
  77. // 检查扫码结果
  78. function checkScanResult(eventId) {
  79. if (eventId !== state.eventId) return;
  80. Fast.api.ajax({
  81. url: 'shopro/wechat/admin/checkScan?event=bind&eventId=' + eventId,
  82. type: 'GET',
  83. loading: false,
  84. }, function (data, res) {
  85. if (res.code === 1) {
  86. // 扫码成功
  87. state.scanStatus = 'scanned';
  88. }
  89. return false;
  90. }, function (data, res) {
  91. if (res.code === -1) {
  92. setTimeout(function () {
  93. checkScanResult(eventId);
  94. }, 2000)
  95. // 待扫码
  96. state.scanStatus = 'pending';
  97. return false;
  98. } else {
  99. // 已过期
  100. state.scanStatus = 'expired';
  101. }
  102. })
  103. }
  104. const qrcodePopoverRef = ref()
  105. // 解除绑定
  106. function onUnbind() {
  107. Fast.api.ajax({
  108. url: 'shopro/wechat/admin/unbind',
  109. type: 'GET',
  110. }, function (data, res) {
  111. state.scanStatus = '';
  112. qrcodePopoverRef.value.hide()
  113. }, function (data, res) {
  114. })
  115. }
  116. onMounted(() => {
  117. getData()
  118. })
  119. return {
  120. state,
  121. getData,
  122. onEdit,
  123. onSetStatus,
  124. onQrcode,
  125. onHideQrcode,
  126. qrcodePopoverRef,
  127. onUnbind,
  128. }
  129. }
  130. }
  131. createApp('index', index);
  132. },
  133. edit: () => {
  134. Controller.form();
  135. },
  136. form: () => {
  137. const { reactive, onMounted, getCurrentInstance } = Vue
  138. const addEdit = {
  139. setup() {
  140. const { proxy } = getCurrentInstance();
  141. const state = reactive({
  142. event: new URLSearchParams(location.search).get('event'),
  143. channel: new URLSearchParams(location.search).get('channel')
  144. })
  145. const form = reactive({
  146. model: {},
  147. rules: {},
  148. })
  149. function getDetail() {
  150. Fast.api.ajax({
  151. url: `shopro/notification/config/detail`,
  152. type: 'GET',
  153. data: {
  154. event: state.event,
  155. channel: state.channel
  156. }
  157. }, function (ret, res) {
  158. form.model = res.data;
  159. if (state.channel == 'Email') {
  160. fieldList.data = res.data.content;
  161. form.model.content = res.data.content_text;
  162. Controller.api.bindevent();
  163. $('#emailContent').html(form.model.content)
  164. } else {
  165. form.model.content.fields.forEach((e) => {
  166. if (!e.value) {
  167. e['value'] = '';
  168. }
  169. if (!e.template_field) {
  170. e['template_field'] = '';
  171. }
  172. });
  173. }
  174. return false
  175. }, function (ret, res) { })
  176. }
  177. const templateIdPopover = reactive({
  178. flag: false,
  179. is_delete: '1'
  180. })
  181. function getTemplateId(is_delete) {
  182. templateIdPopover.flag = false;
  183. Fast.api.ajax({
  184. url: `shopro/notification/config/getTemplateId`,
  185. type: 'GET',
  186. data: {
  187. event: state.event,
  188. channel: state.channel,
  189. is_delete: is_delete,
  190. template_id: is_delete == 1 ? form.model.content.template_id : '',
  191. }
  192. }, function (ret, res) {
  193. form.model.content.template_id = res.data;
  194. }, function (ret, res) { })
  195. }
  196. function onAddField() {
  197. form.model.content.fields.push({
  198. name: '',
  199. template_field: '',
  200. value: '',
  201. });
  202. }
  203. function onDeleteField(index) {
  204. form.model.content.fields.splice(index, 1);
  205. }
  206. const fieldList = reactive({
  207. data: {},
  208. });
  209. function onConfirm() {
  210. proxy.$refs['formRef'].validate((valid) => {
  211. if (valid) {
  212. let submitForm = JSON.parse(JSON.stringify(form.model));
  213. if (state.channel == 'Email') {
  214. delete submitForm.content_text;
  215. submitForm.content = $("#emailContent").val();
  216. }
  217. Fast.api.ajax({
  218. url: `shopro/notification/config/edit`,
  219. type: 'POST',
  220. data: {
  221. event: state.event,
  222. channel: state.channel,
  223. ...submitForm,
  224. }
  225. }, function (ret, res) {
  226. Fast.api.close()
  227. }, function (ret, res) { })
  228. }
  229. });
  230. }
  231. onMounted(() => {
  232. getDetail()
  233. })
  234. return {
  235. state,
  236. form,
  237. templateIdPopover,
  238. getTemplateId,
  239. onAddField,
  240. onDeleteField,
  241. fieldList,
  242. onConfirm
  243. }
  244. }
  245. }
  246. createApp('addEdit', addEdit);
  247. },
  248. api: {
  249. bindevent: function () {
  250. Form.api.bindevent($("form[role=form]"));
  251. },
  252. },
  253. };
  254. return Controller;
  255. });