Pārlūkot izejas kodu

微信通知控制页面

laiguoran 4 gadi atpakaļ
vecāks
revīzija
cab2d0dcc6

+ 2 - 2
app/const/sms_type.js

@@ -1,7 +1,7 @@
 'use strict';
 
 /**
- * 短信通知类型
+ * 短信、微信通知类型
  *
  * @author Ellisran
  * @date 2019/8/20
@@ -11,7 +11,7 @@ const smsConst = {
     TZ: 'TZ',
     JL: 'JL',
     BG: 'BG',
-    XD: 'XD'
+    XD: 'XD',
 };
 const judgeConst = {
     approval: 1,

+ 43 - 1
app/controller/profile_controller.js

@@ -212,7 +212,7 @@ module.exports = app => {
         async smsType(ctx) {
             try {
                 const sessionUser = ctx.session.sessionUser;
-                const result = await ctx.service.projectAccount.smsTypeSet(sessionUser.accountId, ctx.request.body);
+                const result = await ctx.service.projectAccount.noticeTypeSet(sessionUser.accountId, ctx.request.body);
 
                 if (!result) {
                     throw '修改通知类型失败!';
@@ -359,6 +359,48 @@ module.exports = app => {
             };
             await this.layout('profile/safe.ejs', renderData);
         }
+
+        /**
+         * 微信通知
+         *
+         * @param {object} ctx - egg全局变量
+         * @return {void}
+         */
+        async wechat(ctx) {
+            // 获取当前用户数据
+            const sessionUser = ctx.session.sessionUser;
+
+            // 获取账号数据
+            const accountData = await ctx.service.projectAccount.getDataByCondition({ id: sessionUser.accountId });
+
+            const renderData = {
+                accountData,
+                smsType: smsTypeConst.type,
+            };
+            await this.layout('profile/wechat.ejs', renderData, 'profile/wechat_modal.ejs');
+        }
+
+        /**
+         * 微信解绑
+         *
+         * @param {object} ctx - egg全局变量
+         * @return {void}
+         */
+        async removeWechat(ctx) {
+            try {
+                const sessionUser = ctx.session.sessionUser;
+                const result = await ctx.service.projectAccount.bindWx(sessionUser.accountId, null, null);
+
+                if (!result) {
+                    throw '解绑微信失败!';
+                }
+                this.setMessage('微信解绑成功', this.messageType.SUCCESS);
+            } catch (error) {
+                console.log(error);
+                this.setMessage(error.toString(), this.messageType.ERROR);
+            }
+            ctx.redirect(ctx.request.header.referer);
+        }
     }
 
     return ProfileController;

BIN
app/public/images/wechat.png


+ 2 - 0
app/router.js

@@ -312,6 +312,8 @@ module.exports = app => {
     app.post('/profile/code', sessionAuth, 'profileController.getCode');
     app.post('/profile/bind', sessionAuth, 'profileController.bindMobile');
     app.get('/profile/qrCode', sessionAuth, 'profileController.qrCode');
+    app.get('/profile/wechat', sessionAuth, 'profileController.wechat');
+    app.post('/profile/wechat/remove', sessionAuth, 'profileController.removeWechat');
 
     // 标准库相关
     app.post('/std-lib/get-data', sessionAuth, 'standardLibController.getData');

+ 10 - 2
app/service/project_account.js

@@ -620,14 +620,21 @@ module.exports = app => {
          * @param {Number} data - 通知类型
          * @return {Boolean} - 返回修改结果
          */
-        async smsTypeSet(id, data) {
+        async noticeTypeSet(id, data) {
             if (data._csrf !== undefined) {
                 delete data._csrf;
             }
+            const type = parseInt(data.type) === 1 ? 1 : 0; // 对应微信通知和短信通知设置
+            delete data.type;
             const updateData = {
                 id,
-                sms_type: JSON.stringify(data),
             };
+            if (type === 1) {
+                updateData.sms_type = JSON.stringify(data);
+            } else {
+                updateData.wx_type = JSON.stringify(data);
+            }
+            console.log(updateData);
 
             const operate = await this.db.update(this.tableName, updateData);
 
@@ -697,6 +704,7 @@ module.exports = app => {
                 id,
                 wx_openid: openid,
                 wx_name: nickname,
+                wx_type: null,
             };
 
             const operate = await this.db.update(this.tableName, updateData);

+ 4 - 3
app/view/profile/sms.ejs

@@ -46,7 +46,7 @@
                     <div class="mt-5">
                         <h4>通知类型</h4>
                         <p class="text-muted">勾选您需要接收的短信类型。</p>
-                        <form id="sms-form" method="post" action="/profile/sms/type">
+                        <form id="sms-form" method="post" action="/profile/sms/type?csrf=<%- ctx.csrf %>">
                             <input type="hidden" name="_csrf" value="<%= ctx.csrf %>">
                             <% const user_smsType = accountData.sms_type !== '' ? JSON.parse(accountData.sms_type) : null; %>
                             <% for (const s in smsType) { %>
@@ -57,13 +57,14 @@
                                 <div class="col-5">
                                     <% for (const c of smsType[s].children) { %>
                                     <div class="form-check ">
-                                        <input class="form-check-input" type="checkbox" name="<%= s %>[]" value="<%= c.value %>" <% if (user_smsType !== null && user_smsType[s] !== undefined && user_smsType[s].indexOf(c.value.toString()) !== -1) { %>checked<% } %>>
-                                        <label class="form-check-label" for="<%= s %>"><%= c.title %></label>
+                                        <input class="form-check-input" id="<%= s %>_<%- c.value %>" type="checkbox" name="<%= s %>[]" value="<%= c.value %>" <% if (user_smsType !== null && user_smsType[s] !== undefined && user_smsType[s].indexOf(c.value.toString()) !== -1) { %>checked<% } %>>
+                                        <label class="form-check-label" for="<%= s %>_<%- c.value %>"><%= c.title %></label>
                                     </div>
                                     <% } %>
                                 </div>
                             </div>
                             <% } %>
+                            <input name="type" value="1" type="hidden">
                             <button type="submit" class="btn btn-primary btn-sm">确认修改</button>
                         </form>
                     </div>

+ 68 - 0
app/view/profile/wechat.ejs

@@ -0,0 +1,68 @@
+<% include ./sub_menu.ejs %>
+<div class="panel-content" id="app">
+    <div class="panel-title">
+        <div class="title-main">
+            <h2>微信通知</h2>
+        </div>
+    </div>
+    <div class="content-wrap">
+        <div class="c-body">
+            <div class="sjs-height-0">
+            <div class="row m-0">
+                <div class="col-5 my-3">
+                    <% if (accountData.wx_openid !== null && accountData.wx_openid !== '') { %>
+                    <!--已绑定手机-->
+                    <div class="form-group">
+                        <label>微信账号</label>
+                        <input class="form-control-plaintext" readonly="" value="<%= accountData.wx_name %>">
+                        <a href="#remove-wechat" class="btn btn-sm btn-outline-primary" data-toggle="modal" data-target="#remove-wechat">解绑</a>
+                    </div>
+                    <% } else { %>
+                    <div class="form-group">
+                        <label>微信账号</label>
+                        <input class="form-control-plaintext" readonly="" value="未绑定">
+                    </div>
+                    <% } %>
+                    <!--二维码-->
+                    <div class="form-group">
+                        <label>扫码或搜索 关注服务号</label>
+                        <div><img class="w-50" src="/public/images/wechat.png"></div>
+                    </div>
+                    <% if (accountData.wx_openid !== null && accountData.wx_openid !== '') { %>
+                    <!--短信通知开关(已有认证手机后显示)-->
+                    <div class="mt-5">
+                        <h4>通知类型</h4>
+                        <p class="text-muted">勾选您需要接收的微信类型。</p>
+                        <form id="sms-form" method="post" action="/profile/sms/type">
+                            <input type="hidden" name="_csrf" value="<%= ctx.csrf %>">
+                            <% const user_wxType = accountData.wx_type !== '' ? JSON.parse(accountData.wx_type) : null; %>
+                            <% for (const s in smsType) { %>
+                            <div class="form-group row">
+                                <label class="col-auto col-form-label"><%= smsType[s].name %>
+                                    <!--<a href="#sms-view" data-toggle="modal" data-target="#sms-view" class="ml-2"><i class="fa fa-info-circle"></i></a>-->
+                                </label>
+                                <div class="col-5">
+                                    <% for (const c of smsType[s].children) { %>
+                                    <div class="form-check ">
+                                        <input class="form-check-input" id="<%= s %>_<%- c.value %>" type="checkbox" name="<%= s %>[]" value="<%= c.value %>" <% if (user_wxType !== null && user_wxType[s] !== undefined && user_wxType[s].indexOf(c.value.toString()) !== -1) { %>checked<% } %>>
+                                        <label class="form-check-label" for="<%= s %>_<%- c.value %>"><%= c.title %></label>
+                                    </div>
+                                    <% } %>
+                                </div>
+                            </div>
+                            <% } %>
+                            <input name="type" value="0" type="hidden">
+                            <button type="submit" class="btn btn-primary btn-sm">确认修改</button>
+                        </form>
+                    </div>
+                    <% } %>
+                </div>
+            </div>
+            </div>
+        </div>
+    </div>
+</div>
+<script type="text/javascript">
+    const csrf = '<%= ctx.csrf %>';
+</script>
+<script type="text/javascript" src="/public/js/profile.js"></script>

+ 22 - 0
app/view/profile/wechat_modal.ejs

@@ -0,0 +1,22 @@
+<!--短信图示-->
+<div class="modal fade" id="remove-wechat" >
+    <div class="modal-dialog" role="document">
+        <form class="modal-content" method="post" action="/profile/wechat/remove">
+            <div class="modal-header">
+                <h5 class="modal-title">解绑微信账号</h5>
+                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
+                    <span aria-hidden="true">×</span>
+                </button>
+            </div>
+            <div class="modal-body">
+                <h6>确认解绑微信号 <%- accountData.wx_name %>?</h6>
+                <h6>解绑后无法在微信接收通知。</h6>
+            </div>
+            <div class="modal-footer">
+                <input type="hidden" name="_csrf" value="<%= ctx.csrf %>">
+                <button type="button" class="btn btn-sm btn-secondary" data-dismiss="modal">取消</button>
+                <button type="submit" class="btn btn-sm btn-primary">确定解绑</button>
+            </div>
+        </form>
+    </div>
+</div>

+ 5 - 0
config/menu.js

@@ -286,6 +286,11 @@ const profileMenu = {
         display: false,
         url: '/profile/sms',
     },
+    wechat: {
+        name: '微信通知',
+        display: false,
+        url: '/profile/wechat',
+    },
     sign: {
         name: '电子签名',
         display: false,

+ 1 - 0
sql/update.sql

@@ -95,3 +95,4 @@ ADD COLUMN `re_upload` INT NOT NULL DEFAULT 0 COMMENT '是否为审核通过后
 
 ALTER TABLE `zh_project_account` ADD `wx_openid` VARCHAR(50) NULL DEFAULT NULL COMMENT '微信绑定openid' AFTER `bind`;
 ALTER TABLE `zh_project_account` ADD `wx_name` VARCHAR(255) NULL DEFAULT NULL COMMENT '微信昵称' AFTER `wx_openid`;
+ALTER TABLE `zh_project_account` ADD `wx_type` TEXT NULL DEFAULT NULL COMMENT '微信通知类型' AFTER `sms_type`;