DingController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Module\Base;
  4. use App\Module\Project;
  5. use SimpleDingTalk\Config;
  6. use App\Http\Controllers\Controller;
  7. use SimpleDingTalk\User;
  8. use SimpleDingTalk\AccessToken;
  9. use Request;
  10. use DB;
  11. use App\Module\Users;
  12. class DingController extends Controller
  13. {
  14. public function __construct()
  15. {
  16. // 配置信息
  17. $apps=[
  18. 'miniprogram_app' => [
  19. 'info' => [
  20. 'AGENT_ID' => '',
  21. 'APP_KEY' => '',
  22. 'APP_SECRET' => ''
  23. ],
  24. 'access_token' => [
  25. 'expires' => 0,
  26. 'file_path' => ''
  27. ],
  28. 'callback_info' => [
  29. 'aes_key' => '',
  30. 'token' => ''
  31. ],
  32. 'login_info' => [
  33. 'authorize' => [
  34. 'redirect_uri' => '',
  35. 'dingtalk_login_uri' => ''
  36. ]
  37. ],
  38. 'v2' => [
  39. 'access_token' => [
  40. 'expires' => 0,
  41. 'file_path' => ''
  42. ],
  43. ],
  44. 'userAccessToken' => [
  45. 'expires' => 0,
  46. 'file_path' => ''
  47. ]
  48. ],
  49. 'micro_app' => [
  50. 'info' => [
  51. 'AGENT_ID' => '1602970634',
  52. 'APP_KEY' => 'dingoduj8nh4jsroanpr',
  53. 'APP_SECRET' => '9qsEDDVN8sJ96GMe3pqHpiJuEwXnDBHg010kBJJc6GK--HDQ0Cnv5Twuv97ge_JS'
  54. ],
  55. 'access_token' => [
  56. 'expires' => 0,
  57. 'file_path' => base_path('DingTalkToken')
  58. ],
  59. 'callback_info' => [
  60. 'aes_key' => '',
  61. 'token' => ''
  62. ],
  63. 'page' => [
  64. 'app' => '',
  65. 'pc' => '',
  66. 'management' => ''
  67. ],
  68. 'login_info' => [
  69. 'authorize' => [
  70. 'redirect_uri' => '',
  71. 'dingtalk_login_uri'=>''
  72. ]
  73. ],
  74. 'v2' => [
  75. 'access_token' => [
  76. 'expires' => 0,
  77. 'file_path' => ''
  78. ],
  79. ],
  80. 'userAccessToken' => [
  81. 'expires' => 0,
  82. 'file_path' => base_path('DingTalkToken')
  83. ]
  84. ]
  85. ];
  86. $robots=[
  87. 'robot1' => [
  88. 'info' => [
  89. 'AGENT_ID' => 123456,
  90. 'APP_KEY' => '',
  91. 'APP_SECRET' => '',
  92. 'access_token' => '',
  93. 'SEC' => ''
  94. ],
  95. 'access_token' => [
  96. 'expires' => 180,
  97. 'file_path' => './robot.json'
  98. ]
  99. ],
  100. ];
  101. Config::setRobot($robots)->
  102. setApp($apps)->
  103. setAppType('micro_app')->
  104. setRobotType('robot1')->
  105. setCorpId('dingc844bf6147f4d40c35c2f4657eb6378f');
  106. AccessToken::generateToken();
  107. }
  108. public function __invoke($method, $action = '')
  109. {
  110. $app = $method ? $method : 'main';
  111. if ($action) {
  112. $app .= "__" . $action;
  113. }
  114. return (method_exists($this, $app)) ? $this->$app() : Base::ajaxError("404 not found (" . str_replace("__", "/", $app) . ").");
  115. }
  116. public function login()
  117. {
  118. $code = trim(Request::input('code'));
  119. $ret = User::code_getuserinfo($code);
  120. $result = json_decode($ret,true);
  121. if($result['errcode'] === 0){
  122. $get_user_ret = User::get($result['result']['userid']);
  123. $user_result = json_decode($get_user_ret,true);
  124. if($user_result['errcode'] === 0){
  125. $user_info = $user_result['result'];
  126. $user = Base::DBC2A(DB::table('users')->where('username', $user_info['mobile'])->where('nickname', $user_info['name'])->first());
  127. if($user){
  128. $array = [
  129. 'userid' => $result['result']['userid'],
  130. 'token' => Users::token($user),
  131. 'loginnum' => $user['loginnum'] + 1,
  132. 'lastip' => Base::getIp(),
  133. 'lastdate' => Base::time(),
  134. 'lineip' => Base::getIp(),
  135. 'linedate' => Base::time(),
  136. ];
  137. Base::array_over($user, $array);
  138. DB::table('users')->where('id', $user['id'])->update($array);
  139. return Base::retSuccess( "登陆成功!", Users::retInfo($user));
  140. }else{
  141. return Base::retError( "您的姓名为:{$user_info['name']},手机号码为:{$user_info['mobile']},系统里面找不到该用户,请使用账号密码登录!");
  142. }
  143. }else{
  144. return Base::retError( "无法获取钉钉信息,请使用账号密码登录!");
  145. }
  146. }else{
  147. return Base::retError($result['errmsg']);
  148. }
  149. }
  150. public function notice__push()
  151. {
  152. $user = Users::authE();
  153. if (Base::isError($user)) {
  154. return $user;
  155. } else {
  156. $user = $user['data'];
  157. }
  158. $act = trim(Base::getPostValue('act'));
  159. $taskid = intval(Base::getPostValue('taskid'));
  160. $task = Base::DBC2A(DB::table('project_task')
  161. ->where([
  162. ['delete', '=', 0],
  163. ['id', '=', $taskid],
  164. ])
  165. ->first());
  166. if (empty($task)) {
  167. return Base::retError('任务不存在!');
  168. }
  169. if ($task['projectid'] > 0) {
  170. if (!Project::isPersons($task, $user['username'])) {
  171. $inRes = Project::inThe($task['projectid'], $user['username']);
  172. if (Base::isError($inRes)) {
  173. return $inRes;
  174. }
  175. }
  176. if (!in_array($act, ['comment', 'attention'])) {
  177. $checkRole = Project::role('edit_role', $task['projectid'], $task['id']);
  178. if (Base::isError($checkRole)) {
  179. return $checkRole;
  180. }
  181. switch ($act) {
  182. case 'complete':
  183. case 'unfinished':
  184. $checkRole = Project::role('complete_role', $task['projectid'], $task['id']);
  185. if (Base::isError($checkRole)) {
  186. return $checkRole;
  187. }
  188. break;
  189. case 'archived':
  190. case 'unarchived':
  191. $checkRole = Project::role('archived_role', $task['projectid'], $task['id']);
  192. if (Base::isError($checkRole)) {
  193. return $checkRole;
  194. }
  195. break;
  196. case 'delete':
  197. $checkRole = Project::role('del_role', $task['projectid'], $task['id']);
  198. if (Base::isError($checkRole)) {
  199. return $checkRole;
  200. }
  201. break;
  202. }
  203. }
  204. } else {
  205. if (!Project::isPersons($task, $user['username'])) {
  206. return Base::retError('此操作只允许任务负责人!');
  207. }
  208. }
  209. $content = Base::newTrim(Base::getPostValue('content'));
  210. $message = "";
  211. $upArray = [];
  212. $logArray = [];
  213. switch ($act) {
  214. /**
  215. * 修改标题
  216. */
  217. case 'title': {
  218. $upArray['title'] = $content;
  219. $logArray[] = [
  220. 'type' => '日志',
  221. 'projectid' => $task['projectid'],
  222. 'taskid' => $task['id'],
  223. 'username' => $user['username'],
  224. 'detail' => '修改任务标题',
  225. 'indate' => Base::time(),
  226. 'other' => Base::array2string([
  227. 'type' => 'task',
  228. 'id' => $task['id'],
  229. 'title' => $content,
  230. 'old_title' => $task['title'],
  231. ])
  232. ];
  233. break;
  234. }
  235. /**
  236. * 修改子任务
  237. */
  238. case 'subtask': {
  239. if (!is_array($content)) {
  240. $content = [];
  241. }
  242. $subNames = [];
  243. foreach ($content AS $tmp) {
  244. if ($tmp['uname'] && !in_array($tmp['uname'], $subNames)) {
  245. $subNames[] = $tmp['uname'];
  246. }
  247. }
  248. $content = Base::array2string($content);
  249. if ($content == $task['subtask']) {
  250. return Base::retError('子任务未做改变!');
  251. }
  252. $upArray['subtask'] = $content;
  253. //
  254. $detail = '修改子任务';
  255. $subtype = 'modify';
  256. $new_count = count(Base::string2array($content));
  257. $old_count = count(Base::string2array($task['subtask']));
  258. if ($new_count > $old_count) {
  259. $detail = '添加子任务';
  260. $subtype = 'add';
  261. } elseif ($new_count < $old_count) {
  262. $detail = '删除子任务';
  263. $subtype = 'del';
  264. }
  265. //
  266. if ($subNames) {
  267. DB::transaction(function() use ($task, $subNames) {
  268. foreach ($subNames AS $uname) {
  269. $row = Base::DBC2A(DB::table('project_users')->where([
  270. 'type' => '负责人',
  271. 'taskid' => $task['id'],
  272. 'username' => $uname,
  273. ])->lockForUpdate()->first());
  274. if (empty($row)) {
  275. DB::table('project_users')->insert([
  276. 'type' => '负责人',
  277. 'projectid' => $task['projectid'],
  278. 'taskid' => $task['id'],
  279. 'isowner' => $task['username'] == $uname ? 1 : 0,
  280. 'username' => $uname,
  281. 'indate' => Base::time()
  282. ]);
  283. }
  284. }
  285. DB::table('project_users')->where([
  286. 'type' => '负责人',
  287. 'taskid' => $task['id'],
  288. ])->whereNotIn('username', $subNames)->delete();
  289. });
  290. } else {
  291. DB::table('project_users')->where([
  292. 'type' => '负责人',
  293. 'taskid' => $task['id'],
  294. ])->delete();
  295. }
  296. //
  297. $logArray[] = [
  298. 'type' => '日志',
  299. 'projectid' => $task['projectid'],
  300. 'taskid' => $task['id'],
  301. 'username' => $user['username'],
  302. 'detail' => $detail,
  303. 'indate' => Base::time(),
  304. 'other' => Base::array2string([
  305. 'type' => 'task',
  306. 'subtype' => $subtype,
  307. 'id' => $task['id'],
  308. 'title' => $task['title'],
  309. 'subtask' => $content,
  310. 'old_subtask' => $task['subtask'],
  311. ])
  312. ];
  313. break;
  314. }
  315. default: {
  316. return Base::retError('参数错误!');
  317. }
  318. }
  319. }
  320. public function transfer(){
  321. return Base::retSuccess( "登陆成功!");
  322. }
  323. }